package com.hex2int;

public class MacUtils {

	public static final String MAC_SPLIT = ":";

	public static String getMac(String mac0, String mac1){
		return mac0+mac1;
	}

	public static boolean checkMac(String mac){
		return true;
	}

	public static String[] setMac(String mac){
		String str[] = new String[2];
		//TODO
		str[0] = "xxxxxx";
		str[1] = "xxxxxx";
		return str;
	}

	/**
	 * 将MAC地址字符串转换成byte[]数组的形式
	 * <br> E7:F2:7A:01:00:4B:12:00  -->  [-25, -14, 122, 1, 0, 75, 18, 0]
	 * @param mac
	 * @return
	 */
	public static byte [] getMacBytes(String mac){
		byte []macBytes = new byte[8];
		String [] strArr = mac.split(":");

		for(int i = 0;i < strArr.length; i++){
			int value = Integer.parseInt(strArr[i],16);
			macBytes[i] = (byte) value;
		}
		return macBytes;
	}

	/**
	 * 将byte数组转换成String字符串
	 * <br>[-25, -14, 122, 1, 0, 75, 18, 0] --> e7:f2:7a:1:0:4b:12:0
	 * @param macBytes
	 * @return
	 */
	public static String getMacString(byte[] macBytes) {
		String value = "";
		for(int i = 0;i < macBytes.length; i++){
			String sTemp = Integer.toHexString(0xFF &  macBytes[i]);
			value = value+sTemp+":";
		}
		value = value.substring(0,value.lastIndexOf(":"));
		return value;
	}

	/**
	 * zigbee的mac地址是8位的
	 * @param mac
	 * @return
	 */
	public static boolean checkIfn8mac(String mac){
		if(isEmpty(mac)) {
			return false;
		} else {
			String[] split = mac.split(MacUtils.MAC_SPLIT);
			if(split == null) {
				return false;
			}
			if(split.length == 8){
				return true;
			}
		}
		return false;
	}

    /**
     * Returns true if the string is null or 0-length.
     * @param str the string to be examined
     * @return true if str is null or zero length
     */
    public static boolean isEmpty(CharSequence str) {
        if (str == null || str.length() == 0)
            return true;
        else
            return false;
    }
	
	/**
	 * zigbee的mac地址长度是8组
	 * @param mac
	 * @return
	 */
	public static byte [] getZigbeeMacBytes(String mac){
		byte []macBytes = new byte[8];
		String [] strArr = mac.split(":");

		for(int i = 0;i < strArr.length; i++){
			int value = Integer.parseInt(strArr[i],16);
			macBytes[i] = (byte) value;
		}
		return macBytes;
	}

	public static String getZigbeeMacString(byte[] macBytes) {
		String value = "";
		if(macBytes==null||macBytes.length!=8) {
			return value;
		}
		for(int i = 0;i < 8; i++){
			String sTemp = Integer.toHexString(0xFF &  macBytes[i]);
			value = value+sTemp+":";
		}

		value = value.substring(0,value.lastIndexOf(":"));
		System.out.println("value="+value);
		return value;
	}

}
